
package com.wyzz.hr.bo.impl;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;

import com.wyzz.hr.bo.IBenefitTypeBO;
import com.wyzz.hr.dao.IBenefitTypeDAO;
import com.wyzz.hr.pojo.BenefitType;


public class BenefitTypeBOImpl implements IBenefitTypeBO {

	public BenefitTypeBOImpl() {
	}

	public List findAll() {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.BenefitType.class);
		detachedCriteria.setFetchMode("benefitTypeLocNo", FetchMode.JOIN);
		detachedCriteria.addOrder(Order.asc("benefitTypeSortId"));
		return benefitTypeDao.findByCriteria(detachedCriteria);
	}

	public BenefitType searchById(String id) {
		if (id == null)
			return null;
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.BenefitType.class);
		detachedCriteria.add(Restrictions.eq("id", id));
		List list = benefitTypeDao.findByCriteria(detachedCriteria);
		if (list == null || list.size() == 0)
			return null;
		else
			return (BenefitType) list.get(0);
	}

	public List findNameByobj(BenefitType benefitType) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.BenefitType.class);
		dc.add(Restrictions.eq("benefitTypeName", benefitType
				.getBenefitTypeName().trim()));
		List list = benefitTypeDao.findByCriteria(dc);
		return list;
	}

	public List findSameNameByobj(BenefitType benefitType) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.BenefitType.class);
		dc.add(Restrictions.eq("benefitTypeName", benefitType
				.getBenefitTypeName().trim()));
		dc.add(Restrictions.ne("id", benefitType.getId().trim()));
		List list = benefitTypeDao.findByCriteria(dc);
		return list;
	}

	public boolean addBenefitType(BenefitType benefitType) {
		try {
			benefitTypeDao.saveObject(benefitType);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean updateBenefitType(BenefitType benefitType) {
		if (benefitType.getId() == null || benefitType.getId().equals(""))
			return false;
		try {
			benefitTypeDao.updateObject(benefitType);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public boolean delBenefitType(Class name, String id) {
		try {
			List check = new ArrayList();
			boolean isRefed = false;
			String hql = (new StringBuilder()).append(
					"select count(*) from Employee where empBenefitType='")
					.append(id).append("'").toString();
			check = benefitTypeDao.exeHqlList(hql);
			if (Integer.valueOf(check.get(0).toString()).intValue() > 0)
				isRefed = true;
			BenefitType benefitType = (BenefitType) benefitTypeDao.loadObject(
					name, id, null, new boolean[0]);
			if (!isRefed && benefitType != null) {
				benefitTypeDao.deleteObject(benefitType);
				return true;
			}
		} catch (Exception e) {
			return false;
		}
		return false;
	}

	public boolean saveBenefitTypeSortIdByBatch(String ids[]) {
		try {
			int length = ids.length;
			int sortId = 1;
			for (int i = 0; i < length; i++) {
				benefitTypeDao.exeHql((new StringBuilder()).append(
						"update BenefitType set benefitTypeSortId=").append(
						sortId).append(" where id ='").append(ids[i]).append(
						"'").toString());
				sortId++;
			}

			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public IBenefitTypeDAO getBenefitTypeDao() {
		return benefitTypeDao;
	}

	public void setBenefitTypeDao(IBenefitTypeDAO benefitTypeDao) {
		this.benefitTypeDao = benefitTypeDao;
	}

	private IBenefitTypeDAO benefitTypeDao;
}
